<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-card">
        <!-- Logo Section -->
        <div class="logo-section">
          <div class="logo-icon">
            <svg class="logo-symbol" width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
              <rect width="20" height="20" rx="2" fill="white"/>
              <path d="M10 2L2 18H18L10 2Z" fill="#2C2C2C"/>
              <circle cx="10" cy="10" r="3" fill="white"/>
            </svg>
          </div>
          <span class="logo-text">AI教育资源平台</span>
        </div>

        <!-- Header Section -->
        <div class="header-section">
          <h1 class="welcome-title">欢迎使用AI教育资源平台</h1>
          <p class="welcome-subtitle">登录以继续</p>
        </div>

        <!-- Login Form -->
        <form class="login-form" @submit.prevent="handleLogin">
          <div class="form-group">
            <label class="form-label">用户名</label>
            <div class="input-wrapper">
              <input 
                type="text"
                v-model="loginForm.username"
                class="form-input"
                placeholder="请输入用户名"
                required
              />
            </div>
          </div>

          <div class="form-group">
            <label class="form-label">密码</label>
            <div class="input-wrapper">
              <input
                  :type="showPassword ? 'text' : 'password'"
                  v-model="loginForm.password"
                  class="form-input"
                  placeholder="请输入密码"
                  required
              />
              <button
                  type="button"
                  class="password-toggle"
                  @click="togglePassword"
              >
                <el-icon style="font-size: 18px;">
                  <component :is="showPassword ? View :Hide" />
                </el-icon>
              </button>
            </div>
          </div>

          <!-- Form Options -->
          <div class="form-options">
            <label class="remember-me">
              <input 
                type="checkbox" 
                v-model="loginForm.rememberMe"
                class="checkbox"
              />
              <span class="checkbox-label">记住密码</span>
            </label>
            <router-link to="/forget-password" class="forgot-password">忘记密码？</router-link>
          </div>

          <!-- Submit Button -->
          <button type="submit" class="login-button" :disabled="isLoading">
            {{ isLoading ? '登录中...' : '登录' }}
          </button>
        </form>


        <!-- Sign Up Link -->
        <div class="signup-link">
          <span class="signup-text">还没有账号？</span>
          <router-link to="/register" class="signup-button">注册账号</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useAuth } from '@/composables/useAuth'
import { ElMessage, ElIcon } from "element-plus"
import { View, Hide } from '@element-plus/icons-vue'

const router = useRouter()
const { login, getUserHomePage } = useAuth()

// 响应式数据
const isLoading = ref(false)
const showPassword = ref(false)

const loginForm = reactive({
  username: '',
  password: '',
  rememberMe: false
})

// 方法
const togglePassword = () => {
  showPassword.value = !showPassword.value
}

const handleLogin = async () => {
  if (!loginForm.username || !loginForm.password) {
    ElMessage.error('请输入用户名和密码')
    return
  }

  isLoading.value = true
  try {
    const result = await login(loginForm.username, loginForm.password)
    
    if (result.success) {
      ElMessage.success('登录成功')
      
      // 重定向到对应的主页
      const homePage = getUserHomePage()
      await router.push(homePage)
      
    } else {
      ElMessage.error(result.message || '登录失败')
    }
  } catch (error: any) {
    console.error('Login failed:', error)
    ElMessage.error(error.message || '登录失败，请稍后重试')
  } finally {
    isLoading.value = false
  }
}

const loginWithGoogle = () => {
  console.log('Login with Google')
  ElMessage.info('Google登录功能开发中...')
}

const loginWithFacebook = () => {
  console.log('Login with Facebook')
  ElMessage.info('Facebook登录功能开发中...')
}

const goToRegister = () => {
  router.push('/register')
}

const goToForgotPassword = () => {
  router.push('/forget-password')
}
</script>

<style scoped>
.login-page {
  width: 100vw;
  min-height: 100vh;
  background: linear-gradient(90deg, #F8FAFC 0%, #F1F5F9 50%, #E2E8F0 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Inter', sans-serif;
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 448px;
}

.login-card {
  background: white;
  border-radius: 8px;
  border: 1px solid #D9D9D9;
  padding: 48px 32px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}

/* Logo Section */
.logo-section {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 32px;
}

.logo-icon {
  width: 40px;
  height: 40px;
  background: #2C2C2C;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-symbol {
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 2px;
}

.logo-text {
  color: #1E1E1E;
  font-size: 24px;
  font-weight: 700;
  line-height: 32px;
}

/* Header Section */
.header-section {
  text-align: center;
  margin-bottom: 40px;
}

.welcome-title {
  color: #1E1E1E;
  font-size: 30px;
  font-weight: 700;
  line-height: 36px;
  margin: 0 0 12px 0;
}

.welcome-subtitle {
  color: #B3B3B3;
  font-size: 16px;
  font-weight: 400;
  line-height: 24px;
  margin: 0;
}

/* Form Styles */
.login-form {
  margin-bottom: 32px;
}

.form-group {
  margin-bottom: 24px;
}

.form-label {
  display: block;
  color: #1E1E1E;
  font-size: 14px;
  font-weight: 600;
  line-height: 21px;
  margin-bottom: 8px;
}

.input-wrapper {
  position: relative;
}

.form-input {
  width: 100%;
  height: 49px;
  background: white;
  border: 1px solid #D9D9D9;
  border-radius: 8px;
  padding: 0 16px;
  font-size: 16px;
  font-family: 'Inter', sans-serif;
  color: #1E1E1E;
  box-sizing: border-box;
}

.form-input::placeholder {
  color: #999999;
  font-size: 16px;
  font-weight: 400;
}

.form-input:focus {
  outline: none;
  border-color: #2C2C2C;
  box-shadow: 0 0 0 2px rgba(44, 44, 44, 0.1);
}

.password-toggle {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  width: 20px;
  height: 20px;
}

.eye-icon {
  width: 18.33px;
  height: 12.50px;
  background: black;
  border-radius: 2px;
}

/* Form Options */
.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.checkbox {
  width: 16px;
  height: 16px;
  border: 1px solid #D9D9D9;
  border-radius: 4px;
  background: white;
  cursor: pointer;
}

.checkbox-label {
  color: #1E1E1E;
  font-size: 14px;
  font-weight: 400;
  line-height: 21px;
}

.forgot-password {
  color: #2C2C2C;
  font-size: 14px;
  font-weight: 400;
  line-height: 21px;
  text-decoration: none;
}

.forgot-password:hover {
  text-decoration: underline;
}

/* Login Button */
.login-button {
  width: 100%;
  height: 48px;
  background: #2C2C2C;
  border: none;
  border-radius: 8px;
  color: #F5F5F5;
  font-size: 16px;
  font-weight: 400;
  line-height: 24px;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 32px;
}

.login-button:hover:not(:disabled) {
  background: #1a1a1a;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(44, 44, 44, 0.3);
}

.login-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Divider */
.divider {
  position: relative;
  text-align: center;
  margin-bottom: 24px;
}

.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: #D9D9D9;
}

.divider-text {
  background: white;
  color: #B3B3B3;
  font-size: 14px;
  font-weight: 400;
  line-height: 21px;
  padding: 0 16px;
  position: relative;
  z-index: 1;
}

/* Social Login */
.social-login {
  display: flex;
  gap: 12px;
  margin-bottom: 32px;
}

.social-button {
  flex: 1;
  height: 46px;
  background: white;
  border: 1px solid #D9D9D9;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 14px;
  font-weight: 500;
  line-height: 21px;
  color: #1E1E1E;
}

.social-button:hover {
  background: #F8FAFC;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* Google Icon */
.google-icon {
  position: relative;
  width: 20px;
  height: 20px;
}

.google-blue {
  position: absolute;
  width: 8.80px;
  height: 8.62px;
  left: 10px;
  top: 8.33px;
  background: #4285F4;
}

.google-green {
  position: absolute;
  width: 14.25px;
  height: 7.42px;
  left: 1.82px;
  top: 11.75px;
  background: #34A853;
}

.google-yellow {
  position: absolute;
  width: 4.03px;
  height: 8.22px;
  left: 0.83px;
  top: 5.89px;
  background: #FBBC05;
}

.google-red {
  position: absolute;
  width: 14.32px;
  height: 7.42px;
  left: 1.82px;
  top: 0.83px;
  background: #EA4335;
}

/* Facebook Icon */
.facebook-icon {
  width: 20px;
  height: 19.88px;
  background: #1877F2;
  border-radius: 2px;
}

/* Sign Up Link */
.signup-link {
  text-align: center;
}

.signup-text {
  color: #B3B3B3;
  font-size: 14px;
  font-weight: 400;
  line-height: 21px;
  margin-right: 8px;
}

.signup-button {
  color: #2C2C2C;
  font-size: 14px;
  font-weight: 600;
  line-height: 21px;
  text-decoration: none;
}

.signup-button:hover {
  text-decoration: underline;
}

/* Responsive Design */
@media (max-width: 768px) {
  .login-page {
    padding: 16px;
  }
  
  .login-card {
    padding: 32px 24px;
  }
  
  .social-login {
    flex-direction: column;
  }
}

@media (max-width: 480px) {
  .welcome-title {
    font-size: 24px;
    line-height: 30px;
  }
  
  .welcome-subtitle {
    font-size: 14px;
  }
  
  .logo-text {
    font-size: 20px;
  }
}
</style>